package com.project.workjob.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.*;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

public class HttpUtil {

    private String url;

    private Integer timeout;


    public HttpUtil(String url, Integer timeout) {
        this.url = url;
        this.timeout = timeout;
    }

    public GjxlYbHttpResult getGjxlYbUrl(String token) throws Exception {
        try {
            HttpHeaders headers = new HttpHeaders();
            headers.add("Content-Type", "application/json; charset=utf-8");
            headers.add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36");
            headers.add("Authorization", "Bearer " + token);
            HttpEntity httpEntity = new HttpEntity(headers);
            HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
            httpRequestFactory.setConnectionRequestTimeout(this.timeout);
            httpRequestFactory.setConnectTimeout(this.timeout);
            httpRequestFactory.setReadTimeout(this.timeout);
            httpRequestFactory.setBufferRequestBody(false);
            RestTemplate restTemplate = new RestTemplate(httpRequestFactory);
            ResponseEntity<Map> response = restTemplate.exchange(this.url, HttpMethod.GET, httpEntity, Map.class);
            if (response.getStatusCode() != HttpStatus.OK) {
                throw new Exception("请求失败:" + response.getStatusCodeValue());
            }
            GjxlYbHttpResult httpResult = JSONObject.parseObject(JSON.toJSONString(response.getBody()), GjxlYbHttpResult.class);
            return httpResult;
        } catch (Exception e) {
            throw new Exception(e.getMessage());
        }
    }

    public HolidayHttpResult getHolidayUrl() throws Exception {
        try {
            HttpHeaders headers = new HttpHeaders();
            headers.add("Content-Type", "application/json; charset=utf-8");
            headers.add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36");
            HttpEntity httpEntity = new HttpEntity(headers);
            HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
            httpRequestFactory.setConnectionRequestTimeout(this.timeout);
            httpRequestFactory.setConnectTimeout(this.timeout);
            httpRequestFactory.setReadTimeout(this.timeout);
            httpRequestFactory.setBufferRequestBody(false);
            RestTemplate restTemplate = new RestTemplate(httpRequestFactory);
            ResponseEntity<Map> response = restTemplate.exchange(this.url, HttpMethod.GET, httpEntity, Map.class);
            if (response.getStatusCode() != HttpStatus.OK) {
                throw new Exception("请求失败:" + response.getStatusCodeValue());
            }
            HolidayHttpResult httpResult = JSONObject.parseObject(JSON.toJSONString(response.getBody()), HolidayHttpResult.class);
            return httpResult;
        } catch (Exception e) {
            throw new Exception(e.getMessage());
        }
    }
}
